//
//  VisitorMessageLiuyanCell.m
//  ppkc2016
//
//  Created by Laura on 2017/4/13.
//  Copyright © 2017年 拼凑的断音. All rights reserved.
//

#import "VisitorMessageLiuyanCell.h"
#import "XMLVisitorMessageModel.h"
#import "XMLMessageModel.h"
#import "XXTool.h"

@interface VisitorMessageLiuyanCell  ()

@property (nonatomic,strong) UIView *contentBackground;

@property (nonatomic,strong) UIImageView *head;

@property (nonatomic,strong) UILabel *Linfo;

@property (nonatomic,strong) UILabel *time;

@property (nonatomic,strong) UILabel *huifu;

@property (nonatomic) Boolean isVisitor;

@end

@implementation VisitorMessageLiuyanCell
{
     CGFloat rightLabelWidth;
}

-(UIView*)contentBackground
{
    if (_contentBackground==nil)
    {
        _contentBackground = [[UIView alloc] init];
        _contentBackground.layer.shadowOpacity = 1;
        //_contentBackground.layer.shadowColor = [UIColor colorWithRed:214/255.0 green:215/255.0 blue:216/255.0 alpha:1.0].CGColor;
        _contentBackground.layer.shadowColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.3].CGColor;
        _contentBackground.layer.shadowOffset = CGSizeMake(0,1);
        _contentBackground.layer.shadowRadius = 2;
        _contentBackground.layer.masksToBounds = NO;
        [self addSubview:_contentBackground];
    }
    return _contentBackground;
}

-(UIImageView*)head{
    if(_head == nil){
        _head=[[UIImageView alloc]init];
        [_contentBackground addSubview:_head];
    }
    return _head;
}

-(UILabel*)Linfo{
    if(_Linfo == nil){
        _Linfo=[[UILabel alloc]init];
        _Linfo.font = [XXTool systemFontOfSize:13];
        _Linfo.numberOfLines = 0;
        [_contentBackground addSubview:_Linfo];
    }
    return  _Linfo;
}

-(UILabel*)time{
    if(_time == nil){
        _time=[[UILabel alloc]init];
        _time.font = [XXTool systemFontOfSize:13];
        //[UIColor colorWithRed:181/255.0 green:181/255.0 blue:182/255.0 alpha:1.0]
        _time.textColor = [UIColor colorWithRed:89/255.0 green:87/255.0 blue:87/255.0 alpha:1.0];
        [_contentBackground addSubview:_time];
    }
    return _time;
}

-(UILabel*)huifu{
    if(_huifu == nil){
        _huifu=[[UILabel alloc]init];
        _huifu.layer.borderColor = [XXTool colorWithProject].CGColor;
        _huifu.layer.borderWidth = 0.6f;
        _huifu.layer.cornerRadius = 5;
        _huifu.textAlignment = NSTextAlignmentCenter;
        _huifu.text=NSLocalizedString(@"回复",nil);
        _huifu.textColor = [XXTool colorWithProject];
        _huifu.font = [XXTool systemFontOfSize:14];
        _huifu.userInteractionEnabled=YES;
        UITapGestureRecognizer *labelTapGestureRecognizer = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(labelTouchUpInside:)];
        
        [_huifu addGestureRecognizer:labelTapGestureRecognizer];
        
        [_contentBackground addSubview:_huifu];
    }
    return _huifu;
}

-(void)initParameter
{
    rightLabelWidth = 75;
}

-(void)initUI
{
    [self contentBackground];
    [self head];
    [self Linfo];
    [self time];
    [self huifu];
    
}

-(void)refreshLayout:(CGRect)frame{
    CGFloat cnWidth = frame.size.width-rightLabelWidth-20*2-10-60-10;
    _head.frame=CGRectMake(20, 28, 60, 60);
    _Linfo.frame = CGRectMake(20+_head.frame.size.width+20
                                    , 38
                                    , cnWidth
                                    , [XXTool getTextHeightByText:_Linfo.text
                                                             Font:_Linfo.font
                                                            Width:cnWidth]);
    _time.frame = CGRectMake(_Linfo.frame.origin.x
                                        , _Linfo.frame.size.height+_Linfo.frame.origin.y+8
                                        , cnWidth
                                        , 15);
    _contentBackground.frame = CGRectMake(0
                                          , 0
                                          , frame.size.width
                                          , 117);

    _huifu.frame = CGRectMake(_Linfo.frame.origin.x+_Linfo.frame.size.width
                                          , (_contentBackground.frame.size.height-30)/2
                                          , rightLabelWidth
                                          , 30);
    _contentBackground.backgroundColor = [UIColor whiteColor];
    _contentBackground.layer.shadowPath = [UIBezierPath bezierPathWithRect:_contentBackground.frame].CGPath;
}

-(void)setCellWithData:(XMLVisitorMessageModel *)temp
{
    _isVisitor=true;
    _huifu.tag=[temp.orderid integerValue];
     [_head setImageWithUrl:temp.imgUrl placerHolder:[UIImage imageNamed:@"默认头像"]];
    _head.clipsToBounds=YES;
    _head.layer.cornerRadius=_head.frame.size.height/2;
//    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[XXTool stampToTimeWithoutYear:temp.created_at]];
    _time.text = [XXTool stampToTimeWithoutYear:temp.created_at];
    _Linfo.text=temp.value_ex;
    
    [self refreshLayout:self.frame];
}

-(void)setExhiCellWithData:(XMLMessageModel *)temp
{
    _isVisitor=false;
    _huifu.tag=[temp.orderid integerValue];
    [_head setImageWithUrl:temp.headphoto placerHolder:[UIImage imageNamed:@"默认头像"]];
    _head.clipsToBounds=YES;
    _head.layer.cornerRadius=_head.frame.size.height/2;
    //    NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:[XXTool stampToTimeWithoutYear:temp.created_at]];
    _time.text = [XXTool stampToTimeWithoutYear:temp.created_at];
    _Linfo.text=temp.value_ex;
    
    [self refreshLayout:self.frame];
}

/**
 * 点击 回复
 */
-(void)labelTouchUpInside:(UITapGestureRecognizer *)recognizer{
    UILabel *label=(UILabel*)recognizer.view;
    NSInteger orderId=label.tag;
    if(_isVisitor){
         [[NSNotificationCenter defaultCenter] postNotificationName:@"GuanLiuyan" object:nil userInfo:@{@"tag":[NSNumber numberWithInteger:orderId]}];
    }else{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"ExhiLiuyan" object:nil userInfo:@{@"tag":[NSNumber numberWithInteger:orderId]}];
    }
}

@end
